Skip to main content

Convert int to String in Java (with Performance Comparison)

Banner java icon

πŸŽ‰ Java Fun Zone: Converting int to String Like a Pro! πŸš€β€‹

πŸ”₯ The Mission​

You're coding away, deep in thought, and suddenly you need to convert an int to a String. Panic? NEVER! Java has got your back with multiple ways to do it. But which one is the best? Let's dive in and find out! πŸ’‘

πŸ› οΈ The Mighty Conversion Techniques​

Java provides a bunch of ways to transform your humble int into a shiny String. Here are our contenders:

  1. Integer.toString() 🎯 (Fastest & Recommended!)
  2. String.valueOf()
  3. String.format()
  4. String Template Magic (Java 21)

A little Java snippet to set the stage:

int num = 100;

String value1 = Integer.toString(num); // Works for Integer too
String value2 = String.valueOf(num); // Works for Integer too

πŸ₯‡ 1. The Champion: Integer.toString()​

The Integer.toString(int) method takes an int and returns a String representation of it. Simple and blazing fast. ⚑

Assertions.assertEquals("0", Integer.toString(0));
Assertions.assertEquals("40", Integer.toString(40));
Assertions.assertEquals("-40", Integer.toString(-40));

⚠️ Beware of the Limits​

Java int values range from -2,147,483,648 to 2,147,483,647. Go beyond this, and you're in for a surprise! 😲

Assertions.assertEquals("2147483647", Integer.toString(Integer.MAX_VALUE));
Assertions.assertEquals("-2147483648", Integer.toString(Integer.MIN_VALUE));

🎭 Fun with Different Bases​

Want to see your int in different numeral systems? No problem!

Assertions.assertEquals("101000", Integer.toBinaryString(40));  // Binary
Assertions.assertEquals("50", Integer.toOctalString(40)); // Octal
Assertions.assertEquals("28", Integer.toHexString(40)); // Hex

πŸ… 2. String.valueOf() – A Strong Contender​

Works just like Integer.toString(), but calls it internally. Essentially, it's the same thing wearing different clothes. πŸ€·β€β™‚οΈ

Assertions.assertEquals("0", String.valueOf(0));
Assertions.assertEquals("40", String.valueOf(40));
Assertions.assertEquals("-40", String.valueOf(-40));

πŸšΆβ€β™‚οΈ 3. String.format() – Slowpoke Alert! πŸ’β€‹

You could use String.format(), but why bring a bazooka to a knife fight? 🏹

String formatted = String.format("%d", num);  // Works but slow 🐌

πŸ’Ž 4. String Templates (Java 21+) – The Cool New Kid πŸ˜Žβ€‹

With Java 21, we now have string templates!

String s = STR."\{num}";

Looks cool, performs decently, but still not the fastest.


βš”οΈ Performance Battle Royale πŸ”₯​

We put these methods to the test using JMH (Java Microbenchmark Harness). Here’s how they performed:

MethodSyntaxPerformance (Avg Time per Operation)
πŸ† Integer.toString()Integer.toString(int)0.012 Β΅s ⚑
πŸ₯ˆ String.valueOf()String.valueOf(int)0.016 Β΅s
πŸ₯‰ String.format()String.format("%d", int)0.0143 Β΅s 🐒
🎩 String templateSTR."\{i}"0.019 ¡s

πŸ”¬ Why the Difference?​

Here's a sneak peek at the bytecode behind these methods:

  • Integer.toString(): Calls a lightweight static method. 🏎️
  • String.valueOf(): Calls Integer.toString() internally. πŸƒβ€β™‚οΈ
  • String.format(): Uses varargs + boxing. Slow! 🐌
  • String template: Uses InvokeDynamic, but still a tad slower. πŸ€·β€β™‚οΈ

🎯 Conclusion: The Winner Is… πŸ†β€‹

For converting int to String, use:

βœ… Integer.toString(int) β†’ Fastest and best!

βœ… String.valueOf(int) β†’ A close second.

🚫 Avoid String.format() unless you love unnecessary performance hits. πŸ€¦β€β™‚οΈ

πŸš€ String templates are fun, but not the best for performance-critical applications.


πŸŽ‰ Happy Coding​

Now go forth and convert those integers like a Java Ninja! πŸ₯·πŸ’»πŸ”₯


✍️ Tested on an 11th Gen Intel i5-1135G7, Windows 11, Java 21.